package com.ymt.bpm.util;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

/**
 * @Author johnny
 */
public class ImageUtil {

    /**
     * 对图片做裁剪，缩放
     * @param input 源图片
     * @param input 保存的图片
     * @param sourceType 源图片的类型
     * @param savedType 保存图片的类型
     * @param cutX x坐标
     * @param cutY y坐标
     * @param cutWidth 剪切宽度
     * @param cutHeight 剪切高度
     * @param outWidth 输出宽度
     * @param outHeight 输出高度
     */
    public static void cutImageToOutput(InputStream input, OutputStream out,
                                String sourceType, String savedType,
                                int cutX, int cutY, int cutWidth, int cutHeight,
                                int outWidth, int outHeight)
            throws IOException {
        ImageInputStream imageStream = null;
        try {
            //根据指定的类型创建ImageReader
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(sourceType);
            ImageReader reader = readers.next();
            //把需要裁剪的图片读到内存中
            imageStream = ImageIO.createImageInputStream(input);
            reader.setInput(imageStream, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(cutX, cutY, cutWidth, cutHeight);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);

            //ImageIO.write(bi, savedType, out);

            //是否需要缩放
            if (outWidth>0 && outHeight>0 && !(outWidth==cutWidth && outHeight==cutHeight)) {
                BufferedImage zoomImg = new BufferedImage(outWidth, outHeight, BufferedImage.TYPE_INT_RGB);
                //缩放 getScaledInstance(width,height,hints)
                zoomImg.getGraphics().drawImage(bi.getScaledInstance(outWidth, outHeight, Image.SCALE_SMOOTH), 0, 0, null);
                //保存缩放的图片
                ImageIO.write(zoomImg, savedType, out); //写到out
            } else {
                //保存裁剪完的图片
                ImageIO.write(bi, savedType, out); //写到out
            }
        } finally {
            imageStream.close();
        }
    }

    /**
     * 对图片做裁剪，缩放
     * @param input 源图片
     * @param input 保存的图片
     * @param sourceType 源图片的类型
     * @param savedType 保存图片的类型
     * @param cutX x坐标
     * @param cutY y坐标
     * @param cutWidth 剪切宽度
     * @param cutHeight 剪切高度
     * @param outWidth 输出宽度
     * @param outHeight 输出高度
     */
    public static InputStream cutImage(InputStream input,
                                String sourceType, String savedType,
                                int cutX, int cutY, int cutWidth, int cutHeight,
                                int outWidth, int outHeight)
            throws IOException {
        ImageInputStream imageStream = null;
        InputStream isReturn = null;
        try {
            //根据指定的类型创建ImageReader
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(sourceType);
            ImageReader reader = readers.next();
            //把需要裁剪的图片读到内存中
            imageStream = ImageIO.createImageInputStream(input);
            reader.setInput(imageStream, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(cutX, cutY, cutWidth, cutHeight);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);

            //是否需要缩放
            BufferedImage finalImg = bi;
            if (outWidth>0 && outHeight>0 && !(outWidth==cutWidth && outHeight==cutHeight)) {
                BufferedImage zoomImg = new BufferedImage(outWidth, outHeight, BufferedImage.TYPE_INT_RGB);
                //缩放 getScaledInstance(width,height,hints)
                zoomImg.getGraphics().drawImage(bi.getScaledInstance(outWidth, outHeight, Image.SCALE_SMOOTH), 0, 0, null);
                //保存缩放的图片
                finalImg = zoomImg;
            }
            //写到out
            ImageIO.write(finalImg, savedType, imOut);
            isReturn = new ByteArrayInputStream(bos.toByteArray());
        } finally {
            imageStream.close();
        }
        return isReturn;
    }

    public static InputStream getImageStream(ByteArrayOutputStream bos){
        InputStream is = null;
        try {
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);
            is = new ByteArrayInputStream(bos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }

    public static void main(String[] args) throws Exception {
        ImageUtil.cutImageToOutput(new FileInputStream("F:\\DownResources\\素材\\down\\123.jpg"),
                new FileOutputStream("F:\\1.png"),
                "jpg",
                "png",
                135,
                128,
                240,
                240,
                0,
                0);
    }

}
